﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using web.core.ViewModels;
using web.core.Models;
using web.component;

namespace web.core.Controllers
{
    public class admPageStaticController : baseController
    {
        //------------------------------------
        //page_list
        //------------------------------------
        #region page_list PAGE
        public ActionResult page_list()
        {
            bool permission = false;
            InitPage(ref permission);
            ViewBag.permission = permission;
            return View();
        }

        [ChildActionOnly]
        [HttpGet]
        public ActionResult get_list(int? pageindex)
        {
            int? languageId = languageModels.GetLanguageIDbyUrl(Request.Url.PathAndQuery);
            return get_list(pageindex, admin_const.PAGE_LIST_SIZE, languageId);
        }

        [ChildActionOnly]
        [HttpPost]
        public ActionResult get_list(int? pageindex, int? pagesize, int? languageId)
        {
            var lstdelete = Request.Form["is_delete"];
            if (!string.IsNullOrEmpty(lstdelete)) {
                var arr = lstdelete.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                foreach(var iterator in arr){
                    if (iterator != "false")
                        pageStaticModels.deletePage(Convert.ToInt16(iterator));
                }
            } 
            var page_view = new ViewModels.PageStaticPageListViewModels()
            { 
                //list_group = accountModels.getListUserGroup(),
                list_pagesize = GetSizePagingPublic(pagesize ?? admin_const.PAGE_LIST_SIZE),
                pagesize = pagesize ?? admin_const.PAGE_LIST_SIZE
            };
            TryUpdateModel(page_view);
            if (page_view.languageId != null)
            {
                languageId = page_view.languageId;
            }
            var langid = (languageId ?? admin_const.DEFAULT_LANGUAGE_ID);

            var lst_lang = languageModels.get_list_language();
            page_view.select_list_language = new SelectList(lst_lang, "language_id", "language_desc");
            
            page_view.page_list_pstatic = Models.pageStaticModels.adm_page_static_list(pageindex ?? 1, pagesize ?? admin_const.PAGE_LIST_SIZE, langid);
            return PartialView("../Shared/page_static/list_page", page_view);
        }
        //cap nhat trang thai show
        public ActionResult updateStatus(string id)
        {
            //int pid = int.Parse(id.Split(',')[0]);
            //var sid = int.Parse(id.Split(',')[1]);
            //bool isshow = (sid == 0 ? false : true);
            //Models.pageStaticModels.adm_page_statis_enable(pid, isshow);
            //string outStr = "" + sid.ToString();
            string outStr = update_status_pstatic(id, 1);
            return Json(outStr, JsonRequestBehavior.AllowGet);
        }
        //cap nhat trang thai On Menu
        public ActionResult updateOnMenu(string id)
        {
            string outStr = update_status_pstatic(id, 2);
            return Json(outStr, JsonRequestBehavior.AllowGet);
        }
        //cap nhat trang thai cua p static - ajax
        private string  update_status_pstatic(string id, int action){
            int pid = int.Parse(id.Split(',')[0]);
            var sid = int.Parse(id.Split(',')[1]);
            bool isshow = (sid == 0 ? false : true);
            string outStr = "";

            switch(action){
                case 1: //update status
                    Models.pageStaticModels.adm_page_statis_enable(pid, isshow);
                    outStr = "" + sid.ToString();
                    break;
                case 2: // show on menu
                     Models.pageStaticModels.adm_page_statis_on_menu(pid, isshow);
                    outStr = "" + sid.ToString();
                    break;
            }
            return outStr;
        }
        #endregion

        //------------------------------------
        //page_edit
        //------------------------------------
        #region page_edit PAGE
        public ActionResult page_edit()
        {
            bool permission = false;
            InitPage(ref permission);
            ViewBag.permission = permission;
            return View();
        }

        [ChildActionOnly]
        [HttpGet]
        public ActionResult ctr_page_edit(int? id)
        {
            int itemid = id ?? 0;
            if (itemid == 0)
                ViewBag.isAdd = true;
            else
                ViewBag.isAdd = false;

            ViewModels.PageStaticViewModels item_view;
            //var item_view = new ViewModels.LanguageViewModels();
            var it = pageStaticModels.getByID(itemid);
            if (it != null)
            {
                string sUrl = siteSettingSingleModels.GetConfig(global_const.WEBSITE_URL);
                item_view = new PageStaticViewModels()
                {
                    page_id = it.page_id,
                    page_name = it.page_name,
                    link = it.link,
                    image_path = it.image_path,
                    summary = it.summary,
                    page_content = it.page_content,
                    date_modified = it.date_modified,
                    show = it.show,
                    language_id = it.language_id?? admin_const.DEFAULT_LANGUAGE_ID,
                    keyword = it.keyword,
                    description = it.description,
                    title = it.title,
                    order_display = it.order_display,
                    image_display = Ufile.LoadImage(sUrl, it.image_path), 
                    on_menu = it.on_menu
                    //listLanguage = languageModels.get_list_language()
                };
            }
            else
                item_view = new PageStaticViewModels()
                {
                    show = admin_const.DEFAULT_ITEM_STATUS_B,
                    on_menu = admin_const.DEFAULT_ITEM_STATUS_B,
                    language_id = admin_const.DEFAULT_LANGUAGE_ID,
                    order_display = admin_const.DEFAULT_ORDER_DISPLAY,
                    //listLanguage = languageModels.get_list_language()
                };

            var lst_lang = languageModels.get_list_language();
            item_view.select_list_language = new SelectList(lst_lang, "language_id", "language_desc", item_view.language_id);
            item_view.select_list_status = new List<SelectListItem>() {  
                    new SelectListItem{Selected=item_view.show == true?true:false, Text = "Kích hoạt", Value = "true" },
                    new SelectListItem{Selected=item_view.show == false?true:false, Text = "Vô hiệu hóa", Value = "false" }
            };
            item_view.select_list_on_menu = new List<SelectListItem>() {  
                    new SelectListItem{Selected=item_view.on_menu == true?true:false, Text = "Có", Value = "true" },
                    new SelectListItem{Selected=item_view.on_menu == false?true:false, Text = "Không", Value = "false" }
            };
            

            return PartialView("../Shared/page_static/edit_page", item_view);
        }

        [ChildActionOnly]
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult ctr_page_edit(FormCollection collection)
        {
            var item_view = new ViewModels.PageStaticViewModels();
            TryUpdateModel(item_view);

            if (string.IsNullOrEmpty(item_view.page_name) || string.IsNullOrEmpty(item_view.link))
            {
                ViewBag.result_message = "Tên và Link của trang không để trống.";
                ViewBag.result = false;
                return PartialView("../Shared/page_static/edit_page", item_view);
            }
            bool isdel = item_view.page_id != 0 ? true : false;
            var item = new data.entities.page_static()
            {
                page_id = item_view.page_id,
                page_name = item_view.page_name,
                link = item_view.link,
                //image_path = item_view.image_path,
                image_path = SaveImage(isdel, item_view.image_path, item_view.fileUpload, admin_const.E_IMG_TYPE.PAGE_STATIC, admin_const.E_IMG_THUMB.NO_THUMB),
                summary = item_view.summary,
                page_content = item_view.page_content,
                date_modified = Udate_time.GetCurrentDateTime(),
                show = item_view.show,
                language_id = item_view.language_id,
                keyword = item_view.keyword,
                description = item_view.description,
                title = item_view.title,
                order_display = item_view.order_display,
                on_menu = item_view.on_menu
            };

            int ret_id = pageStaticModels.adm_page_static_update(item);

            if (ret_id > 0)
            {
                ViewBag.result_message = admin_const.UPDATE_OK;
                ViewBag.redirect = "/vi/admPageStatic/page_list";
                return PartialView("../Shared/control_redirect");
            }
            else
            {
                ViewBag.result_message = admin_const.UPDATE_FAIL;
                ViewBag.result = false;
                return PartialView("../Shared/page_static/edit_page", item);
            }
        }
        //tao ra link ao 
        public ActionResult getlink(string id)
        {
            string text = id;
            string outStr = Ustring.RemoveIllegalCharacters(id);
            return Json(outStr);
        }
        #endregion
    }
}